import axios from "axios";
import router from "../router/index";
import store from "../store";
class HttpRequest {

    interceptors(instance) {
        // 添加请求拦截器
        instance.interceptors.request.use(function (config) {
            // 在发送请求之前做些什么
            // console.log('来了？');
            // config.headers.Authorization = localStorage.getItem('token')
            // console.log(store.state.token);

            return config;
        }, function (error) {
            // 对请求错误做些什么
            return Promise.reject(error);
        });
        // 添加响应拦截器
        instance.interceptors.response.use(function (response) {
            // 对响应数据做点什么
            // console.log('来了啊');
            // console.log(response);

            if (response.data.code === 401) {
                router.push('/')
                console.log('token过期');
            }
            return response;
        }, function (error) {
            // 对响应错误做点什么
            return Promise.reject(error);
        });
    }
    request(option) {
        const instance = axios.create({
            baseURL: 'http://49.232.18.137:8000',
            timeont: 150000,
            headers: {
                // 'Authorization':  localStorage.getItem('token'),
            }
        })
        this.interceptors(instance)
        return instance(option)
    }
}
export default new HttpRequest